#include <soc.h>
#include <yoc/yoc.h>
#include "upacker.h"
#include <aos/aos.h>
#include "aos/cli.h"
#include <debug/dbg.h>
#include <stdio.h>
#include <pthread.h>

uint8_t upacker_stable[10]={1,2,3,4,5,6,7,8,9,10};
uint8_t upacker_rtable[20]={0};
upacker_inst msg_packer = {0};
int flag = 0;

static void uart_send(uint8_t *d, uint16_t size)
{
    //使用数据拷贝代替
    if(flag == 0){
        memcpy(&upacker_rtable,d,size);
        flag = 1;
    }
    else{
        memcpy(&upacker_rtable[4],d,size);
        flag = 0;
    }
}

static void handle_cb(uint8_t *d, uint16_t size)
{
    aos_debug_printf("pack len%d", size);
}

static void * upacker_thread(void *arg)
{
    upacker_init(&msg_packer, handle_cb, uart_send);
    upacker_pack(&msg_packer, (uint8_t *)upacker_stable, 10);
    upacker_unpack(&msg_packer, (uint8_t *)upacker_rtable, 10);  
    sleep(1);
    return 0;
}

static int upacker_example(int argc, char *argv[])
{
    int ret = 0;
    int thread_para;
    pthread_t new_thread;
    pthread_attr_t attr;
    int *retval;

    thread_para = 10;
    pthread_attr_init(&attr);
    pthread_attr_setstacksize(&attr, 4096);
    ret = pthread_create(&new_thread, &attr, upacker_thread, &thread_para);
    if (ret != 0) {
        printf("pthread_create failed, ret:%d\n", ret);
        pthread_attr_destroy(&attr);
        return -1;
    }
    return 0;
}

ALIOS_CLI_CMD_REGISTER(upacker_example, upacker, upacker test example);